<template>
	<view class="scroll-view-container" v-if="cateGoryList.length">
		<view class="left-scroll-view">
			<view :class="['left-scroll-view-item', cateIndex === index?'active':'']"
				v-for="(item,index) in cateGoryList" :key="item.cat_id" @click="onChange(index)">
				{{ item.cat_name}}
			</view>
		</view>
		<view class="right-scroll-view" v-if="cateGoryRightList.length>0">
			<!-- 二级分类 -->
			<view v-for="item in cateGoryRightList" :key="item.cat_id">
				<view class="cate-lv2-title">{{item.cat_name}}</view>
				<!-- 三级标题和三级分类 -->
				<view class="cate-lv3-list">
					<view class="cate-lv3-item" v-for="subItem in item.children" :key="subItem.cat_id">
						<image :src="subItem.cat_icon" mode="" style="width:60px;height:60px" />
						<view class="name">{{subItem.cat_name}}</view>
					</view>
				</view>
			</view>
		</view>
		<van-empty v-else description="暂无二级分类" />
	</view>
</template>

<script>
	import {
		getCateGories
	} from '../../api/category'
	export default {
		data() {
			return {
				cateGoryList: [],
				cateIndex: 0, //高亮索引
				cateGoryRightList: []

			}
		},
		onLoad() {
			this.getCateGoryList()
		},
		methods: {
			async getCateGoryList() {
				const res = await getCateGories()
				this.cateGoryList = res.data
				this.cateGoryRightList = res.data[0].children
			},

			// 点击改变
			onChange(index) {
				this.cateIndex = index
				// console.log(this.cateIndex, index);
				this.cateGoryRightList = this.cateGoryList[index].children ?? []
				console.log(this.cateGoryRightList)
			}
		}
	}
</script>

<style scoped lang="scss">
	.scroll-view-container {
		display: flex;
		height: 100%;

		.left-scroll-view {
			width: 120px;
			height: 100vh;
			overflow: auto;

			.left-scroll-view-item {
				line-height: 60px;
				background-color: #f7f7f7;
				text-align: center;
				font-size: 12px;

				// 激活项的样式
				&.active {
					background-color: #ffffff;
					position: relative;
					color: #c00000;
				}
			}
		}

		// 去除滚动条
		.left-scroll-view::-webkit-scrollbar {
			width: 0 !important;
		}

		.right-scroll-view {
			height: 100vh;
			overflow: auto;
			flex: 1;

			.cate-lv2-title {
				font-size: 12px;
				font-weight: bold;
				padding: 15px 15px;
			}

			.cate-lv3-list {
				display: flex;
				flex-wrap: wrap;

				.cate-lv3-item {
					width: 33.33%;
					margin-bottom: 10px;
					display: flex;
					flex-direction: column;
					align-items: center;

					image {
						width: 120rpx;
						height: 120rpx;
					}

					.name {
						font-size: 12px;
						color: gray;
					}
				}
			}
		}

		// 去除滚动条
		.right-scroll-view::-webkit-scrollbar {
			width: 0 !important;
		}
	}
</style>